//
//  HomePageItemCell.m
//  ffmpeg_demo
//
//  Created by hxh on 2021/2/4.
//

#import "MinePageOpusListCell.h"
@interface MinePageOpusListCell ()
@property (nonatomic,strong)UIImageView *imgView;
@property (nonatomic,strong)UIImageView *contextImgView;
@property (nonatomic,strong)UIImageView *likeIcon;
@property (nonatomic,strong)UIImageView *typeIcon;
@property (nonatomic,strong)UILabel *mainContentLb;
@property (nonatomic,strong)UILabel *subContentLb;
@property (nonatomic,strong)UIView *coverView;
@property (nonatomic,strong)UILabel *numberLb;
@property (nonatomic,strong)UIView *line;
@property (nonatomic,strong)UIImageView *watchIcon;
@property (nonatomic,strong)UILabel *watchNumberLb;
@property (nonatomic,strong)UIButton *coverBtn;
@property (nonatomic,strong)UILabel *createTimeLb;
@property (nonatomic,strong)UIView *titleView;
@property (nonatomic,strong)UILabel *titleLb;
@end

@implementation MinePageOpusListCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 4;
    self.contentView.layer.masksToBounds = YES;

        
    self.coverBtn = [[UIButton alloc]init];
    [self.coverBtn addTarget:self action:@selector(coverMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.coverBtn];

    self.imgView = [[UIImageView alloc]init];
    self.imgView.backgroundColor = RGBA(240, 240, 240, 1);
    self.imgView.contentMode = UIViewContentModeScaleAspectFill;
    self.imgView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.imgView];
    
    self.contextImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"homePage_tag_context_bg"]];
    [self.contentView addSubview:self.contextImgView];
    

    
    self.subContentLb = [[UILabel alloc]init];
    self.subContentLb.text = @"xxxx";
    self.subContentLb.numberOfLines = 2;
    self.subContentLb.textColor = C51;
    self.subContentLb.font = [UIFont boldSystemFontOfSize:12];
    [self.contentView addSubview:self.subContentLb];
    

    self.mainContentLb = [[UILabel alloc]init];
    self.mainContentLb.text = @"xxxxxx";
    self.mainContentLb.textColor = RGBA(51, 51, 51, 1);
    self.mainContentLb.font = [UIFont systemFontOfSize:12];
    self.mainContentLb.numberOfLines = 0;
    self.mainContentLb.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.mainContentLb];

    self.coverView = [[UIView alloc]init];
    self.coverView.layer.cornerRadius = 9;
    self.coverView.layer.masksToBounds = YES;
    self.coverView.backgroundColor = RGBA(0, 0, 0, 0.1);
    self.coverView.userInteractionEnabled = NO;
    [self.contentView addSubview:self.coverView];
    
    self.typeIcon = [[UIImageView alloc]init];
    self.typeIcon.image = [UIImage imageNamed:@"home_tag_video"];
    [self.coverView addSubview:self.typeIcon];
    
    self.numberLb = [[UILabel alloc]init];
    self.numberLb.text = @"";
    self.numberLb.textColor = UIColor.whiteColor;
    self.numberLb.font = font(10);
    [self.coverView addSubview:self.numberLb];
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(255, 255, 255, 0.3);
    [self.coverView addSubview:self.line];
    
    self.watchIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_tag_watch"]];
    [self.contentView addSubview:self.watchIcon];
    [self.coverView addSubview:self.watchIcon];
    
    self.watchNumberLb = [[UILabel alloc]init];
    self.watchNumberLb.text = @"";
    self.watchNumberLb.textColor = UIColor.whiteColor;
    self.watchNumberLb.font = font(10);
    [self.coverView addSubview:self.watchNumberLb];
    
    
    self.createTimeLb = [[UILabel alloc]init];
    self.createTimeLb.text = @"2022-12-12";
    self.createTimeLb.textColor = C153;
    self.createTimeLb.font = font(12);
    [self.contentView addSubview:self.createTimeLb];
    
    self.titleView = [[UIView alloc]init];
    self.titleView.backgroundColor = RGBA(0, 0, 0, 0.5);
    [self.contentView addSubview:self.titleView];
    
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.text = @"审核中";
    self.titleLb.textColor = [UIColor whiteColor];
    self.titleLb.font = font(10);
    [self.titleView addSubview:self.titleLb];
    
    [self.createTimeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.subContentLb.mas_bottom).offset(8);
        make.left.equalTo(self.subContentLb);
    }];
    

    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.contentView);
    }];
    
    CGFloat rate = (CGFloat)(4.0f/3);
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.contentView);
        make.height.mas_equalTo(self.imgView.mas_width).multipliedBy(rate);
    }];
    
    [self.contextImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.imgView);
    }];
    
    [self.subContentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imgView.mas_bottom).offset(12);
        make.left.equalTo(self.contentView).offset(8);
        make.right.equalTo(self.contentView).offset(-8);
    }];
    
    [self.mainContentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(8);
        make.right.equalTo(self.contentView).offset(-8);
        make.top.equalTo(self.contentView).offset(32);
        make.bottom.equalTo(self.imgView.mas_bottom).offset(-56);
    }];
    

    [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.imgView.mas_bottom).offset(-12);
        make.left.equalTo(self.imgView.mas_left).offset(8);
        make.height.mas_equalTo(18);
    }];
    
    

    [self.typeIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.coverView);
        make.left.equalTo(self.coverView).offset(8);
    }];

    [self.numberLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.typeIcon.mas_right).offset(0);
        make.centerY.equalTo(self.coverView);
    }];
 
 
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.coverView).offset(5);
        make.bottom.equalTo(self.coverView).offset(-5);
        make.left.equalTo(self.numberLb.mas_right).offset(6);
        make.width.mas_equalTo(1);
    }];
    

    [self.watchIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.line.mas_right).offset(6);
        make.centerY.equalTo(self.coverView);//////
    }];
    

    [self.watchNumberLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.watchIcon.mas_right).offset(4);
        make.centerY.equalTo(self.coverView);
        make.right.equalTo(self.coverView).offset(-4);
    }];
    
    
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(42, 20));
        make.left.top.equalTo(self.contentView);
    }];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.titleView);
    }];


}

- (void)coverMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showOpusDetailWithCurrentModel:)]) {
        [self.delegate showOpusDetailWithCurrentModel:self.itemModel];
    }
}


- (void)setItemModel:(HomePageItemModel *)itemModel{
    _itemModel = itemModel;
    
    if ([[RPAppManager getCustomerId] isEqualToNumber:itemModel.userId]) {
        self.titleView.hidden = itemModel.isDisplay.intValue == 1;
    }else{
        self.titleView.hidden = YES;
    }
    
    self.createTimeLb.text = itemModel.createDetailTimeStr;
    // 文本
    if (itemModel.contentType.intValue == 1) {
        self.subContentLb.hidden = YES;
        self.subContentLb.text = nil;
        self.mainContentLb.hidden = NO;
        self.mainContentLb.text = itemModel.contentText;
    // 其他
    }else{
        self.subContentLb.hidden = NO;
        self.mainContentLb.hidden = YES;
        self.subContentLb.text = itemModel.contentText;
    }

    
    if (itemModel.contentType.intValue == 1) {
        self.numberLb.text = @"";
        self.typeIcon.image = [UIImage imageNamed:@"homePage_tag_context"];
        self.imgView.hidden = YES;
        self.numberLb.hidden = YES;
        self.contextImgView.hidden = NO;
    }else if (itemModel.contentType.intValue == 2){
        self.typeIcon.image = [UIImage imageNamed:@"homePage_tag_pic"];
        self.imgView.hidden = NO;
        self.numberLb.hidden = NO;
        self.contextImgView.hidden = YES;
        self.numberLb.text = [NSString stringWithFormat:@"%lu",(unsigned long)itemModel.contents.count];
        HomePageContentModel *obj = itemModel.contents.firstObject;
        [self.imgView sd_setImageWithURL:[NSURL URLWithString:obj.postUrl]];
    }else if (itemModel.contentType.intValue == 3){
        self.numberLb.text = @"";
        self.typeIcon.image = [UIImage imageNamed:@"homePage_tag_video"];
        self.imgView.hidden = NO;
        self.numberLb.hidden = YES;
        self.contextImgView.hidden = YES;
        __block HomePageContentModel *item = nil;
        [itemModel.contents enumerateObjectsUsingBlock:^(HomePageContentModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            // 图片
            if (obj.seq.intValue == 1) {
                item = obj;
                *stop = YES;
            }
        }];
        [self.imgView sd_setImageWithURL:[NSURL URLWithString:item.postUrl]];
    }


  
    if (itemModel.watchNum.intValue >= 10000) {
        self.watchNumberLb.text = [NSString stringWithFormat:@"%@w",[RPAppManager notRounding:itemModel.watchNum.intValue/10000.0f afterPoint:1]];
    }else{
        if (itemModel.watchNum.intValue == 0) {
            self.watchNumberLb.text = @"";
        }else{
            self.watchNumberLb.text = [NSString stringWithFormat:@"%@",itemModel.watchNum];
        }
    }
    
    if (itemModel.watchNum.intValue > 0) {
        [self.watchNumberLb mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.coverView).offset(-8);
        }];
    }else{
        [self.watchNumberLb mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.coverView).offset(-4);
        }];
    }
    if (itemModel.contentType.intValue == 2) {
        [self.numberLb mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.typeIcon.mas_right).offset(4);
        }];
    }else{
        [self.numberLb mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.typeIcon.mas_right).offset(0);
        }];
    }
    self.likeIcon.highlighted = itemModel.isLike;
    

    if (itemModel.contentText.length == 0 || itemModel.contentType.intValue == 1) {
        [self.createTimeLb mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.subContentLb.mas_bottom);
        }];
    }else{
        [self.createTimeLb mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.subContentLb.mas_bottom).offset(8);
        }];
    }
}

@end
